import { region } from '../../common/layout/region.js'
export default {
    data: {
//        通讯录数组
        mailList: [],
        province: [],// 省
        currentProvince: {}, // 当前省
        city: [],// 市
        currentCity: {}, // 当前市
        county: [],// 县
        currentCounty: {}, // 当前县
        currentType: 'province',
        flag: false
    },
    onInit() {
        this._getProvince();
    },
    clickInfo(item) {
        if (item.detail.type === 'province') {
            this.currentProvince = item.detail;
            this.currentType = 'city';
            this._getCity();
        } else if (item.detail.type === 'city') {
            this.currentCity = item.detail;
            this.currentType = 'county';
            this._getCounty();
        } else if (item.detail.type === 'county') {
            this.currentCounty = item.detail;
            this.currentType = 'end';
        }
    },
    changeChoice(type) {
        if (type === 'province') {
            if (this.currentType === 'province') return false;
            this.currentType = 'province';
            this.currentCity = {};
            this.currentCounty = {};
            this.mailList = this.province;
            setTimeout(() => {
                this.$child('mail_list').myInit();
            }, 0)
        } else if (type === 'city') {
            if (this.currentType === 'city') return false;
            this.currentType = 'city';
            this.currentCounty = {};
            this._getCity();
        } else if (type === 'county') {
            if (this.currentType === 'county') return false;
            this.currentType = 'county';
            this._getCounty();
        }
    },
//    获取省数据
    _getProvince() {
        let province = [];
        console.log('获取省数据')
        region.forEach((item, index) => {
            province.push({
                id: item.ssqid,
                title: item.ssqname,
                type: 'province',
                index: index
            })
        })
        this.province = province;
        this.mailList = province;
        this.currentType = 'province';
        this.currentProvince = {};
        this.currentCity = {};
        this.currentCounty = {};
        setTimeout(() => {
            this.flag = true
        }, 0)
        setTimeout(() => {
            this.$child('mail_list').myInit();
        }, 200)
    },
//    获取市数据
    _getCity() {
        let city = [];
        this.currentType = 'city';
        region[this.currentProvince.index].cities.city.forEach((item, index) => {
            city.push({
                id: item.ssqid,
                title: item.ssqname,
                type: 'city',
                index: index
            })
        })
        this.city = city;
        this.mailList = city;
        setTimeout(() => {
            this.$child('mail_list').myInit();
        }, 0)
        this.currentCity = {};
        this.currentCounty = {};
    },
//    获取县数据
    _getCounty() {
        let county = [];
        region[this.currentProvince.index].cities.city[this.currentCity.index].areas.area.forEach(item => {
            county.push({
                id: item.ssqid,
                title: item.ssqname,
                type: 'county'
            })
        })
        this.county = county;
        this.mailList = county;
        setTimeout(res => {
            this.$child('mail_list').myInit();
        }, 0)
        this.currentType = 'county';
        this.currentCounty = {};
    }
}
